Coverage Report

Created: 2025-05-07 21:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
D:\a\tools.proto\tools.proto\compiler\src\compiler\builder.rs
Line
Count
Source
1
// Copyright (c) 2024, BlockProject 3D
2
//
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without modification,
6
// are permitted provided that the following conditions are met:
7
//
8
//     * Redistributions of source code must retain the above copyright notice,
9
//       this list of conditions and the following disclaimer.
10
//     * Redistributions in binary form must reproduce the above copyright notice,
11
//       this list of conditions and the following disclaimer in the documentation
12
//       and/or other materials provided with the distribution.
13
//     * Neither the name of BlockProject 3D nor the names of its contributors
14
//       may be used to endorse or promote products derived from this software
15
//       without specific prior written permission.
16
//
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
use crate::compiler::message::{Field, FieldType, HeaderField, SizeInfo};
30
use crate::model::protocol::{Description, Endianness};
31
32
pub struct FieldBuilder {
33
    name: String,
34
    optional: bool,
35
    endianness: Endianness,
36
    header: Option<HeaderField>,
37
    codec: Option<String>,
38
    size_info: SizeInfo,
39
    description: Option<Description>,
40
}
41
42
impl FieldBuilder {
43
182
    pub fn new(name: String, optional: bool, endianness: Endianness) -> Self {
44
182
        Self {
45
182
            name,
46
182
            description: None,
47
182
            endianness,
48
182
            optional,
49
182
            header: None,
50
182
            codec: None,
51
182
            size_info: SizeInfo {
52
182
                is_element_dyn_sized: false,
53
182
                is_dyn_sized: true,
54
182
            },
55
182
        }
56
182
    }
57
58
0
    pub fn name(&self) -> &str {
59
0
        &self.name
60
0
    }
61
62
86
    pub fn has_codec(&self) -> bool {
63
86
        self.codec.is_some()
64
86
    }
65
66
182
    pub fn description(mut self, description: Option<Description>) -> Self {
67
182
        self.description = description;
68
182
        self
69
182
    }
70
71
182
    pub fn header(mut self, header: Option<HeaderField>) -> Self {
72
182
        self.header = header;
73
182
        self
74
182
    }
75
76
266
    pub fn codec(mut self, codec: Option<String>) -> Self {
77
266
        self.codec = codec;
78
266
        self
79
266
    }
80
81
32
    pub fn size_info(mut self, size_info: SizeInfo) -> Self {
82
32
        self.size_info = size_info;
83
32
        self
84
32
    }
85
86
48
    pub fn fixed_size(mut self) -> Self {
87
48
        self.size_info = SizeInfo {
88
48
            is_dyn_sized: false,
89
48
            is_element_dyn_sized: false,
90
48
        };
91
48
        self
92
48
    }
93
94
30
    pub fn dynamic_size(mut self) -> Self {
95
30
        self.size_info = SizeInfo {
96
30
            is_dyn_sized: true,
97
30
            is_element_dyn_sized: true,
98
30
        };
99
30
        self
100
30
    }
101
102
176
    pub fn build(self, ty: FieldType) -> Field {
103
176
        Field {
104
176
            name: self.name,
105
176
            header: self.header,
106
176
            ty,
107
176
            optional: self.optional,
108
176
            size: self.size_info,
109
176
            endianness: self.endianness,
110
176
            description: self.description,
111
176
            codec: self.codec,
112
176
        }
113
176
    }
114
}